package com.estar.assistsurvy.fragment;

import java.util.ArrayList;
import java.util.Set;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.activity.EWorkCardActivity;
import com.estar.assistsurvy.activity.HistoryActivity;
import com.estar.assistsurvy.activity.LoginActivity;
import com.estar.assistsurvy.activity.SettingsActivity;
import com.estar.assistsurvy.activity.WalletActivity;
import com.estar.assistsurvy.adapter.MyPageListviewAdapter;
import com.estar.assistsurvy.constant.Constants;
import com.estar.assistsurvy.entity.Item;
import com.estar.assistsurvy.utils.GlideCircleTransform;
import com.estar.assistsurvy.widget.NotScrollListview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MinePageFragment extends BaseFragment implements OnClickListener,OnItemClickListener{

	private ImageView headImg;
	private TextView userName,telephone;
	private Button logOut;
	private NotScrollListview myPageList;
	private MyPageListviewAdapter adapter;
	private Intent intent;
	private SharedPreferences preference,prefer;
	private RequestManager glideRequest;
	private String authenState,headUrl,roleCode;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(view==null){
			view=inflater.inflate(R.layout.fragment_minepage,null);
		}
		initView();
		return view;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initView();
	}

	private void initView(){
		prefer=getActivity().getSharedPreferences("role", Context.MODE_PRIVATE);
		roleCode=prefer.getString("rolecode", "");
		preference=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
		authenState=preference.getString("authenticationState", "");
		headUrl=preference.getString("headUrl", "");
		headImg=find(R.id.headImg);
		glideRequest = Glide.with(this);
//		String url="http://i0.sinaimg.cn/lx/2012/0411/U3606P8DT20120411102936.jpg";
//		glideRequest.load(url).transform(new GlideCircleTransform(getActivity())).into(headImg);
//		if(headUrl.equals("")){
//			glideRequest.load(R.drawable.icon_head).transform(new GlideCircleTransform(getActivity())).into(headImg);
//		}else{
			glideRequest.load(Constants.PIC_PATH+headUrl)
			.diskCacheStrategy(DiskCacheStrategy.ALL)
			.placeholder(R.drawable.icon_head)
			.transform(new GlideCircleTransform(getActivity()))
			.into(headImg);
//		}
				
		//		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.psb8);
		//		bitmap=BitmapUtils.toRoundBitmap(bitmap);
		//		headImg.setImageBitmap(bitmap);
		userName=find(R.id.userName);
		userName.setText(preference.getString("realName", ""));
		telephone=find(R.id.telephone);
		telephone.setText(preference.getString("phoneNumber", ""));
		logOut=find(R.id.logOut);
		logOut.setOnClickListener(this);
		myPageList=find(R.id.myPageList);
		adapter=new MyPageListviewAdapter(setData(), getActivity());
		myPageList.setAdapter(adapter);
		myPageList.setOnItemClickListener(this);
	}

	private ArrayList<Item> setData(){
		ArrayList<Item> list=new ArrayList<Item>();
		int[] imgs=new int[]{R.drawable.icon_wallet2,R.drawable.icon_mission,R.drawable.icon_setting,
				R.drawable.icon_ecard,R.drawable.icon_contactus};
		String[] txts=new String[]{"钱包","我的任务","设置","电子工牌","联系我们"};
		for(int i=0;i<imgs.length;i++){
			Item item=new Item();
			item.setImg(imgs[i]);
			item.setText(txts[i]);
			list.add(item);
		}
		return list;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			intent=new Intent(getActivity(), WalletActivity.class);
			startActivity(intent);
			break;
			
		case 1:
			if(authenState.equals("0")){
				showShortToast("您还未认证，请先认证!");
				return;
			}
			if(roleCode.equals("6")){
				showShortToast("您无此权限进入该功能!");
				return;
			}
			intent=new Intent(getActivity(), HistoryActivity.class);
			startActivity(intent);
			break;
			
		case 2:
			intent=new Intent(getActivity(), SettingsActivity.class);
			startActivity(intent);
			break;

		case 3:
			if(authenState.equals("0")){
				showShortToast("您还未认证，请先认证!");
				return;
			}
			if(roleCode.equals("6")){
				showShortToast("您无此权限进入该功能!");
				return;
			}
			intent=new Intent(getActivity(), EWorkCardActivity.class);
			startActivity(intent);
			break;
			
		case 4:
			new AlertDialog.Builder(getActivity(),AlertDialog.THEME_HOLO_LIGHT)
			.setTitle("提示")
			.setMessage("是否要拨打长安保险客服电话?")
			.setPositiveButton("是", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+"95532"));
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}
				
			})
			.setNegativeButton("否", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
				
			})
			.create()
			.show();
			
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.logOut:
			new AlertDialog.Builder(getActivity(),AlertDialog.THEME_HOLO_LIGHT)
			.setTitle("确定退出吗？")
			.setPositiveButton("是", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					preference.edit().clear().commit();
					prefer.edit().clear().commit();
					JPushInterface.setAliasAndTags(getActivity().getApplicationContext(), "", null,mAliasCallback);
					intent=new Intent(getActivity(), LoginActivity.class);
					startActivity(intent);
					getActivity().finish();
				}
			})
			.setNegativeButton("否", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub

				}

			})
			.create()
			.show();

			break;

		default:
			break;
		}
	}
	
	private final TagAliasCallback mAliasCallback = new TagAliasCallback() {  
		@Override  
		public void gotResult(int code, String alias, Set<String> tags) {  
			//	        String logs ;  
			switch (code) {  
			case 0:  
					          String  logs = "Set tag and alias success";  
					            Log.i("wl", logs+"code--->"+code+"----alias--->"+alias);  
				// 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。  
//				SharedPreferences preference=getActivity().getSharedPreferences("alias", Context.MODE_PRIVATE);
//				Editor editor=preference.edit();
//				editor.putString("deviceToken", JPushInterface.getRegistrationID(getActivity().getApplicationContext()));
//				editor.commit();
				break;  
			case 6002:  

				break;  
			default:  
			}   
		}  
	}; 
}
